#pragma once
#include <iostream>
#include <string>
#include <cstdio>
#include <set>
#include "Command.hpp"
#include "InetAddr.hpp"
#include "Log.hpp"

using namespace LogModule;

class Command
{

public:
    Command()
    {
        // 严格匹配
        _WhiteListCommands.insert("ls");
        _WhiteListCommands.insert("pwd");
        _WhiteListCommands.insert("ls -l");
        _WhiteListCommands.insert("touch haha.txt");
        _WhiteListCommands.insert("who");
        _WhiteListCommands.insert("whoami");
    }
    bool IsSafeCommand(const std::string &cmd)
    {
        auto iter = _WhiteListCommands.find(cmd);
        return iter != _WhiteListCommands.end();
    }
    std::string Execute(const std::string &cmd,InetAddr &addr)
    {
        if(!IsSafeCommand(cmd))
        {
            return std::string("error!!!!!!!!!");
        }

        std::string who = addr.StringAddr();

        //用于在C程序中执行shell命令并与之进行单向管道通信
        FILE* fp = popen(cmd.c_str(),"r");
        if(fp == nullptr)
        {
            return std::string("你要执行的命令不存在！") + cmd;
        }
        std::string res;
        char line[1024];
        while(fgets(line,sizeof(line),fp))
        {
            res+=line;
        }
        pclose(fp);
        std::string result = who + "execute done, result is:\n" + res;
        LOG(LogLevel::DEBUG) << result;
        return result;
    }
    ~Command()
    {}
private:
    std::set<std::string> _WhiteListCommands;
};